package cn.biplam.front.component.view.mdManage.buffer2md.main;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.mdManage.TypeCode.OptModelIndexCode;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.BufCompTabCode;
import cn.biplam.common.struc.BaseStruc;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.mdManage.MdObjectSelect;
import cn.biplam.front.component.data.mdManage.MdObjectSelect.Show_Type;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.AjxScript;

/**
 * 表主题修改
 * @author WangSong 2009-7-3上午09:07:38
 * @version 1.0
 */
public class Buffer2MdThemeModify extends AjxResponse{
	
	public static final String cId = "2211";
	public static final String aId = "221101";
	public static final String aId_modify = "221102";
	private String modelType;
	private String themeId;
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(aId.equals(actionId)){
			getParams();
			return get();
		}else if(aId_modify.equals(actionId)){
			return this.modifyOpt();
		}else{
			throw new ErrorMessage("Buffer2MdThemeModify.java,actionId is not found:"+actionId+"!");
		}
	}
	
	/**
	 * 修改页面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow get() throws ErrorMessage{
		HtmlWindow theme = new HtmlDiv();
		MdObjectSelect mos = new MdObjectSelect(this.themeId,aId_modify);
		mos.setModelType(MdStruc.getEnumModelType(modelType));
		mos.setShowType(Show_Type.Theme);
		mos.putAdditionParams("ctid", recordId);
		mos.putAdditionParams("taskId", this.params.getByKey("taskId"));
		theme.put(mos.get(sHomeUrl, mUser));
		return theme;
	}
	
	/**
	 * 取页面传过来的参数，并进行处理
	 * @throws ErrorMessage 
	 */
	private void getParams() throws ErrorMessage{
		/** recordId是比较表的行id及ctid，根据 ctid取任务id*/
		this.modelType = this.params.getByKey("modelType");
		this.themeId = this.params.getByKey("themeId");
		getIndexThemeID();
	}
	
	/**
	 * 取索引表中的主题索引ID
	 * @throws ErrorMessage
	 */
	private void getIndexThemeID() throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(BaseStruc.IndexTableStruc.NODE_CODE,this.themeId);
		filter.put(BaseStruc.IndexTableStruc.NODE_TYPE,this.modelType);
		OperatorResult sr = Operator.get(OptModelIndexCode.GetBy_RealId_And_ModelType, 
				        mUser.getUserID(), filter);
		if(TOOLS.exist(sr)){
			this.themeId = sr.getValue(BaseStruc.IndexTableStruc.NODE_ID);
		}
	}
	
	/**
	 * 修改操作
	 * @return HtmlWindow
	 * @throws ErrorMessage 
	 */
	private HtmlWindow modifyOpt() throws ErrorMessage{
		HtmlWindow modify = new HtmlDiv();
		this.updateTheme();
		this.bindScript(modify);
		return modify;
	}
	
	/**
	 * 更新主题
	 * @throws ErrorMessage 
	 */
	private void updateTheme() throws ErrorMessage{
		String ctid = this.params.getByKey("ctid");
		String themeId = recordId;
		
		DataFilter filter = new DataFilter();
		filter.put(MdStruc.ImpCompTabStruc.CTID,ctid);
		filter.put(MdStruc.ImpCompTabStruc.THEME,themeId);
		Operator.get(BufCompTabCode.Update, mUser.getUserID(), filter);
	}
	
	/**
	 * 绑定刷新脚本
	 * @param modify   绑定脚本的容器
	 * @throws ErrorMessage
	 */
	private void bindScript(HtmlWindow modify) throws ErrorMessage{
		String taskId = this.params.getByKey("taskId");
		//生成提交脚本,提交完后刷新当前页面
		String script = AjxScript.getSp(Buffer2MdCompareTable.cId, //提交容器ID
				Buffer2MdCompareTable.aId,                         //提交动作ID
				Buffer2MdCompareTable.cId,                         //父容器ID
				taskId                                             //主键ID
				);
		modify.bind(script);
	}
}
